﻿using API_Service.DBHelper;
using Dapper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

namespace API_Service.BizLogic.BasicData
{
    public class LineGroupService:BaseLogicService
    {
        public ExecutionResult<dynamic> GetSectionNameList()
        {
            var result = new ExecutionResult<dynamic>();
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    string sql = "SELECT distinct [section_name] FROM [dbo].[c_group_config_t] WHERE [is_valid] ='Y'";
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Open();
                    }

                    result.Datas = conn.Query(sql).ToArray();
                    result.Status = true;
                    result.Message = "Query DB Group name list success.";
                    Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + " " + result.Message);
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "查询系统站别信息异常";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }

            }
            return result;
        }
        public ExecutionResult<dynamic> GetGroupConfigList(string vGroupName = null, string vSectionName =null)
        {
            var result = new ExecutionResult<dynamic>();
            #region SQL and parameters
            string sql = @"SELECT [group_code]
                                                  ,[group_name]
                                                  ,[section_name]
                                                  ,[description]
                                                  ,[remarks]
                                                  ,[is_valid]
                                                  ,[create_user]
                                                  ,[create_time]
                                                   ,[update_user]                                                 
                                                  ,[update_time]
                                              FROM [dbo].[c_group_config_t] WHERE 1=1";
            DynamicParameters dbParams = new DynamicParameters();
            if (!String.IsNullOrEmpty(vGroupName))
            {
                sql = sql + " AND [group_name] LIKE @group_name";
                dbParams.Add("@group_name", String.Format("%{0}%", vGroupName), DbType.String, ParameterDirection.Input);
            }
            if (!String.IsNullOrEmpty(vSectionName))
            {
                sql = sql + " AND [section_name] LIKE @section_name";
                dbParams.Add("@section_name", String.Format("%{0}%", vSectionName), DbType.String, ParameterDirection.Input);
            }
            sql = sql + " ORDER BY [group_code] ASC";
            
            #endregion
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                  
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Open();
                    }

                    result.Datas = conn.Query(sql, dbParams).ToArray();
                    result.Status = true;
                    result.Message = "Query DB Group  list success.";
                    Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + " " + result.Message);
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "查询系统站别信息异常";                    
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }

            }
            return result;
        }

        public ExecutionResult<dynamic> AddGroupCfgInfo(dynamic vModel)
        {
            ExecutionResult<dynamic> result;
            result = new ExecutionResult<dynamic>();
            var model = JsonConvert.DeserializeObject(vModel);

            result = this.GetGroupConfigList((string)model.group_name, (string)model.section_name);
            if (result.Status)
            {
                if (result.Datas.Length > 0)
                {
                    result.Status = false;
                    result.Message = "站别[" + (string)model.group_name + "]已经存在";                
                }
                else
                {
                    result = this.InsertGroupCfgInfo(vModel);
                }
            }

            return result;
        }
        private ExecutionResult<dynamic> InsertGroupCfgInfo(dynamic vModel)
        {
            ExecutionResult<dynamic> result;
            result = new ExecutionResult<dynamic>();
            var model = JsonConvert.DeserializeObject(vModel);

            #region SQL and parameters
            DynamicParameters dbParams = new DynamicParameters();
            string sql = @"INSERT INTO [dbo].[c_group_config_t]
                                           ([group_name],[section_name],[description],[remarks],[create_time],[create_user])
                                     VALUES(@group_name,@section_name,@description,@remarks,GETDATE(),@create_user)";
            dbParams.Add("@group_name", (string)model.group_name, DbType.String, ParameterDirection.Input);
            dbParams.Add("@section_name", (string)model.section_name, DbType.String, ParameterDirection.Input);
            dbParams.Add("@description", (string)model.description, DbType.String, ParameterDirection.Input);
            dbParams.Add("@remarks", (string)model.remarks, DbType.String, ParameterDirection.Input);
            dbParams.Add("@create_user", (string)model.create_user, DbType.String, ParameterDirection.Input);            
            
            #endregion
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    result.ExecuteCount = conn.Execute(sql, dbParams);
                    if (result.ExecuteCount > 0)
                    {
                        result.Status = true;
                        result.Message = "insert group config info success.";
                    }
                    else
                    {
                        result.Status = false;
                        result.Message = "insert group cofnig info fail.";
                    }
                    Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + "--" + result.Message);
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "Insert group config exception. Msg:" + ex.Message;
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            return result;
        }

        public ExecutionResult<dynamic> UpdateGroupCfgInfo(dynamic vModel)
        {
            ExecutionResult<dynamic> result;
            result = new ExecutionResult<dynamic>();
            var model = JsonConvert.DeserializeObject(vModel);
            #region SQL and parameters
            string sql = @"UPDATE [dbo].[c_group_config_t]
                                       SET [group_name]=@group_name,[section_name]=@section_name,
                                              [description]=@description, [remarks]=@remarks,
                                        update_time=GETDATE(),[update_user]=@update_user
                                    WHERE [group_code]=@group_code";
            DynamicParameters dbParams = new DynamicParameters();
            dbParams.Add("@group_name", (string)model.group_name, DbType.String, ParameterDirection.Input);
            dbParams.Add("@section_name", (string)model.section_name, DbType.String, ParameterDirection.Input);
            dbParams.Add("@description", (string)model.description, DbType.String, ParameterDirection.Input);
            dbParams.Add("@remarks", (string)model.remarks, DbType.String, ParameterDirection.Input);
            dbParams.Add("@update_user", (string)model.update_user, DbType.String, ParameterDirection.Input);
            dbParams.Add("@group_code", (string)model.group_code, DbType.String, ParameterDirection.Input);
            #endregion
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    result.ExecuteCount = conn.Execute(sql, dbParams);
                    if(result.ExecuteCount > 0)
                    {
                        result.Status = true;
                        result.Message = "update group config info success.";
                    }
                    else
                    {
                        result.Status = false;
                        result.Message = "update group config info fail.";
                    }
                    Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + "--" + result.Message);
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "update group config exception. Msg:" + ex.Message;
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            return result;
        }

        public ExecutionResult<dynamic> DeleteGroupCfgInfo(int vGroupCode)
        {
            ExecutionResult<dynamic> result;
            result = new ExecutionResult<dynamic>();
            
            #region SQL and parameters
            string sql = @"DELETE FROM [dbo].[c_group_config_t]
                                    WHERE group_code=@group_code";
            #endregion
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();
                    result.ExecuteCount = conn.Execute(sql, new { group_code = vGroupCode });
                    if (result.ExecuteCount > 0)
                    {
                        result.Status = true;
                        result.Message = "delete group config info success.";
                    }
                    else
                    {
                        result.Status = false;
                        result.Message = "delete group config info fail.";
                    }
                    Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + "--" + result.Message);
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "delete group config exception. Msg:" + ex.Message;
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            return result;
        }

        public ExecutionResult<dynamic> GetLineNameList()
        {
            var result = new ExecutionResult<dynamic>();
            List<string> _lineNames = new List<string>();
            result = this.GetLineBaseCfgInfo();
            if (result.Status)
            {
                foreach(var line in result.Datas)
                {
                    if (!_lineNames.Contains(line.line_name))
                    {
                        _lineNames.Add(line.line_name);
                    }
                }
            }

            result.Datas = _lineNames.ToArray();
            result.Status = true;
            result.Message = "Get line name list success.";

            return result;
        }

        public ExecutionResult<dynamic> GetLineBaseCfgInfo(string vLineName =null, string vShift = null, int vLineId = 0)
        {
            var result = new ExecutionResult<dynamic>();
            var loopup = new Dictionary<string, dynamic>();
            string sql = @"SELECT * FROM [dbo].[c_line_base_info_t]  AS a 
                                        LEFT JOIN [dbo].[c_line_manager_info_t] AS b 
                                            ON a.line_id = b.line_id
                                        WHERE 1=1 ";
            var dbParams = new DynamicParameters();
            if (!string.IsNullOrEmpty(vLineName))
            {
                sql = sql + " AND a.[line_name] LIKE @line_name";
                //dbParams.Add("@line_name", vLineName, DbType.String, ParameterDirection.Input);
                dbParams.Add("@line_name", string.Format("%{0}%", vLineName), DbType.String, ParameterDirection.Input);
            }
            if (!string.IsNullOrEmpty(vShift))
            {
                sql = sql + " AND a.[shift] =@shift";
                dbParams.Add("@shift", vShift, DbType.String, ParameterDirection.Input);
            }
            if(vLineId > 0)
            {
                sql = sql + " AND a.line_id=@line_id";
                dbParams.Add("@line_id", vLineId, DbType.Int32, ParameterDirection.Input);
            }
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {                    
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Open();
                    }
                    var list = conn.Query<dynamic, dynamic, dynamic>(sql,
                        (lb, lm) => { lb.Linemanager = lm; return lb; }, dbParams, null, true, splitOn: "line_id").ToList();

                    result.Datas = list.ToArray();
                    result.Status = true;
                    result.Message = "Query DB line base info list success.";
                    Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + " " + result.Message);
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "查询线别出勤信息异常";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }

            }
            return result;
        }

        public ExecutionResult<dynamic> AddLineBaseInfo(dynamic vModel)
        {
            var result =new ExecutionResult<dynamic>();
            var vm = JsonConvert.DeserializeObject<dynamic>(vModel);

            if (vm != null)
            {
                Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + " Start add line base info. line name is " + vm.line_name);
                //check line base exists or not
                result = this.GetLineBaseCfgInfo(Convert.ToString(vm.line_name));
                if (result.Status && result.Datas.Length >0)
                {
                    result.Status = false;
                    result.Message = "线体信息已经存在，不能重复添加。";
                    return result;
                }
                result = this.InsertLineBaseCfgInfo(vm);
                if (result.Status)
                {
                    var line_id = result.ExecuteCount;
                    var lineManager = vm.linemanager;
                    if (lineManager != null)
                    {
                        lineManager.line_id = line_id;
                        result = this.InsertLineMngrCfgInfo(lineManager);
                    }
                }
            }
            else
            {
                result.Status = false;
                result.Message = "参数传递错误..";
            }
            
            return result;
        }

        public ExecutionResult<dynamic> UpdateLineBaseInfo(dynamic vModel)
        {
            var result = new ExecutionResult<dynamic>();

            var vm = JsonConvert.DeserializeObject<dynamic>(vModel);

            if (vm != null)
            {
                Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + " Start update line base info. line id is " + vm.line_id);
                //check line base exists
                result = this.GetLineBaseCfgInfo(null, null, Convert.ToInt32(vm.line_id));
                if(result.Status && result.Datas.Length > 0)
                {
                    result = this.UpdateLineBaseCfgInfo(vm);
                    if (result.Status)
                    {
                        result = this.GetLineMngrCfgInfo(Convert.ToInt32(vm.linemanager.line_id));

                        if (result.Status && result.Datas.Length > 0)
                        {
                            result = this.UpdateLineMngrCfgInfo(vm.linemanager);
                        }
                      else
                        {
                            result = this.InsertLineMngrCfgInfo(vm.linemanager);
                        }
                    }
                }                
            }
            else
            {
                result.Status = false;
                result.Message = "参数传递错误..";
            }

            return result;
        }

        public ExecutionResult<dynamic> DeleteLineBaseInfo(int vId)
        {
            var result = new ExecutionResult<dynamic>();
            if (vId > 0)
            {
                Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + " Delete line base info. line id is " + vId);

                result = this.DeleteLineBaseCfgInfo(vId);
                if (result.Status)
                {
                    result = this.DeleteLineMngrCfgInfo(vId);
                }
            }
            else
            {
                result.Status = false;
                result.Message = "参数传递错误..";
            }

            return result;
        }

        private ExecutionResult<dynamic> InsertLineBaseCfgInfo(dynamic vModel)
        {
            var result = new ExecutionResult<dynamic>();
            #region DB Sql and paramters --Insert Line Base Info
            var dbParams = new DynamicParameters();
            string sql = @"INSERT INTO [dbo].[c_line_base_info_t]([line_name],[shift],[line_speed],[announcement],
                                        [hc_budget],[hc_actual],[absent],[line_description],[remarks],[is_valid],[create_time],[create_user])
                                    VALUES(@line_name,@shift,@line_speed,@announcement,@hc_budget,@hc_actual,@absent,
                                                    @line_description,@remarks,@is_valid,GETDATE(),@create_user); SELECT @lineid = SCOPE_IDENTITY()";
            dbParams.Add("@line_name", (string)vModel.line_name, DbType.String, ParameterDirection.Input);
            dbParams.Add("@shift", (string)vModel.shift, DbType.String, ParameterDirection.Input);
            dbParams.Add("@line_speed", (string)vModel.line_speed, DbType.String, ParameterDirection.Input);
            dbParams.Add("@announcement", (string)vModel.announcement, DbType.String, ParameterDirection.Input);
            dbParams.Add("@hc_budget", (Int32)vModel.hc_budget, DbType.Int32, ParameterDirection.Input);
            dbParams.Add("@hc_actual", (Int32)vModel.hc_actual, DbType.Int32, ParameterDirection.Input);
            dbParams.Add("@absent", (Int32)vModel.absent, DbType.Int32, ParameterDirection.Input);
            dbParams.Add("@line_description", (string)vModel.line_description, DbType.String, ParameterDirection.Input);
            dbParams.Add("@remarks", (string)vModel.remarks, DbType.String, ParameterDirection.Input);
            dbParams.Add("@is_valid", "Y", DbType.String, ParameterDirection.Input);
            dbParams.Add("@create_user", (string)vModel.create_user, DbType.String, ParameterDirection.Input);
            dbParams.Add("@lineid", 5, DbType.Int32, ParameterDirection.Output);
            #endregion

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();

                    var res = conn.Execute(sql, dbParams);
                    result.ExecuteCount = dbParams.Get<int>("@lineid");
                    result.Status = true;
                    result.Message = "新增线体配置信息成功.";

                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "新增线体配置信息异常.";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            return result;
        }

        private ExecutionResult<dynamic> UpdateLineBaseCfgInfo(dynamic vModel)
        {
            var result = new ExecutionResult<dynamic>();
            #region DB Sql and paramters
            var dbParams = new DynamicParameters();
            string sql = @"UPDATE [dbo].[c_line_base_info_t] SET [line_name]=@line_name,[shift]=@shift
                                          ,[line_speed]=@line_speed,[announcement]=@announcement,[hc_budget]=@hc_budget,[hc_actual]=@hc_actual,[absent]=@absent
                                          ,[line_description]=@line_description,[remarks]=@remarks,[is_valid]=@is_valid,[update_time]=GETDATE(),[update_user]=@update_user
                                    WHERE [line_id]=@line_id";
            dbParams.Add("@line_name", (string)vModel.line_name, DbType.String, ParameterDirection.Input);
            dbParams.Add("@shift", (string)vModel.shift, DbType.String, ParameterDirection.Input);
            dbParams.Add("@line_speed", (string)vModel.line_speed, DbType.String, ParameterDirection.Input);
            dbParams.Add("@announcement", (string)vModel.announcement, DbType.String, ParameterDirection.Input);
            dbParams.Add("@hc_budget", (Int32)vModel.hc_budget, DbType.Int32, ParameterDirection.Input);
            dbParams.Add("@hc_actual", (Int32)vModel.hc_actual, DbType.Int32, ParameterDirection.Input);
            dbParams.Add("@absent", (Int32)vModel.absent, DbType.Int32, ParameterDirection.Input);
            dbParams.Add("@line_description", (string)vModel.line_description, DbType.String, ParameterDirection.Input);
            dbParams.Add("@remarks", (string)vModel.remarks, DbType.String, ParameterDirection.Input);
            dbParams.Add("@is_valid", "Y", DbType.String, ParameterDirection.Input);
            dbParams.Add("@update_user", (string)vModel.update_user, DbType.String, ParameterDirection.Input);
            dbParams.Add("@line_id", (Int32)vModel.line_id, DbType.Int32, ParameterDirection.Input);
            #endregion

            using(IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();

                    result.ExecuteCount = conn.Execute(sql, dbParams);
                    result.Status = true;
                    result.Message = "更新线体配置信息成功.";
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "更新线体配置信息异常.";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            return result;
        }

        private ExecutionResult<dynamic> DeleteLineBaseCfgInfo(int vId)
        {
            var result = new ExecutionResult<dynamic>();
            #region DB Sql and paramters
            var dbParams = new DynamicParameters();
            string sql = @"DELETE FROM [dbo].[c_line_base_info_t] WHERE [line_id]=@line_id";
            dbParams.Add("@line_id", vId, DbType.Int32, ParameterDirection.Input);
            #endregion

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();

                    result.ExecuteCount = conn.Execute(sql, dbParams);
                    result.Status = true;
                    result.Message = "删除线体配置信息成功.";
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "删除线体配置信息异常.";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            return result;
        }

        private ExecutionResult<dynamic> InsertLineMngrCfgInfo(dynamic vModel)
        {
            var result = new ExecutionResult<dynamic>();
            #region DB Sql and paramters
            var dbParams = new DynamicParameters();
            string sql = @"INSERT INTO [dbo].[c_line_manager_info_t]
                                       ([line_id]
                                       ,[line_manager]
                                       ,[manager_tel_no]
                                       ,[manager_email]
                                       ,[manager_picture]
                                       ,[line_leader]
                                       ,[leader_tel_no]
                                       ,[leader_email]
                                       ,[leader_picture]
                                       ,[remarks]
                                       ,[is_valid]
                                       ,[create_time]
                                       ,[create_user])
                                 VALUES(@line_id,@line_manager,@manager_tel_no,@manager_email,@manager_picture,
                                               @line_leader,@leader_tel_no,@leader_email,@leader_picture,
                                                    @remarks,@is_valid,GETDATE(),@create_user)";
            dbParams.Add("@line_id", (Int32)vModel.line_id, DbType.Int32, ParameterDirection.Input);
            dbParams.Add("@line_manager",(string) vModel.line_manager, DbType.String, ParameterDirection.Input);
            dbParams.Add("@manager_tel_no", (string)vModel.manager_tel_no, DbType.String, ParameterDirection.Input);
            dbParams.Add("@manager_email", (string)vModel.manager_email, DbType.String, ParameterDirection.Input);
            dbParams.Add("@manager_picture", (string)vModel.manager_picture, DbType.String, ParameterDirection.Input);
            dbParams.Add("@line_leader", (string)vModel.line_leader, DbType.String, ParameterDirection.Input);
            dbParams.Add("@leader_tel_no", (string)vModel.leader_tel_no, DbType.String, ParameterDirection.Input);
            dbParams.Add("@leader_email", (string)vModel.leader_email, DbType.String, ParameterDirection.Input);
            dbParams.Add("@leader_picture", (string)vModel.learder_picture, DbType.String, ParameterDirection.Input);
            dbParams.Add("@remarks", (string)vModel.remarks, DbType.String, ParameterDirection.Input);
            dbParams.Add("@is_valid", (string)vModel.is_valid, DbType.String, ParameterDirection.Input);
            dbParams.Add("@create_user", (string)vModel.create_user, DbType.String, ParameterDirection.Input);
            #endregion

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();

                    result.ExecuteCount = conn.Execute(sql, dbParams);
                    result.Status = true;
                    result.Message = "更新线体管理者信息成功.";
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "更新线体管理者信息异常.";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            return result;
        }
        private ExecutionResult<dynamic> UpdateLineMngrCfgInfo(dynamic vModel)
        {
            var result = new ExecutionResult<dynamic>();
            #region DB Sql and paramters
            var dbParams = new DynamicParameters();
            string sql = @"UPDATE [dbo].[c_line_manager_info_t] SET [line_manager]=@line_manager,[manager_tel_no]=@manager_tel_no
                                          ,[manager_email]=@manager_email,[manager_picture]=@manager_picture,[line_leader]=@line_leader
                                          ,[leader_tel_no]=@leader_tel_no,[leader_email]=@leader_email
                                          ,[leader_picture]=@leader_picture,[remarks]=@remarks,[is_valid]=@is_valid
                                          ,[update_time]=GETDATE(),[update_user]=@update_user
                                    WHERE [line_id]=@line_id";
            dbParams.Add("@line_id", (Int32)vModel.line_id, DbType.Int32, ParameterDirection.Input);
            dbParams.Add("@line_manager", (string)vModel.line_manager, DbType.String, ParameterDirection.Input);
            dbParams.Add("@manager_tel_no", (string)vModel.manager_tel_no, DbType.String, ParameterDirection.Input);
            dbParams.Add("@manager_email", (string)vModel.manager_email, DbType.String, ParameterDirection.Input);
            dbParams.Add("@manager_picture", (string)vModel.manager_picture, DbType.String, ParameterDirection.Input);
            dbParams.Add("@line_leader", (string)vModel.line_leader, DbType.String, ParameterDirection.Input);
            dbParams.Add("@leader_tel_no", (string)vModel.leader_tel_no, DbType.String, ParameterDirection.Input);
            dbParams.Add("@leader_email", (string)vModel.leader_email, DbType.String, ParameterDirection.Input);
            dbParams.Add("@leader_picture", (string)vModel.leader_picture, DbType.String, ParameterDirection.Input);
            dbParams.Add("@remarks", (string)vModel.remarks, DbType.String, ParameterDirection.Input);
            dbParams.Add("@is_valid", (string)vModel.is_valid, DbType.String, ParameterDirection.Input);
            dbParams.Add("@update_user", (string)vModel.update_user, DbType.String, ParameterDirection.Input);            
            #endregion

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();

                    result.ExecuteCount = conn.Execute(sql, dbParams);
                    result.Status = true;
                    result.Message = "更新线体管理者信息成功.";
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "更新线体管理者信息异常.";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            return result;
        }
        private ExecutionResult<dynamic> DeleteLineMngrCfgInfo(int vLineId)
        {
            var result = new ExecutionResult<dynamic>();
            #region DB Sql and paramters
            var dbParams = new DynamicParameters();
            string sql = @"DELETE FROM [dbo].[c_line_manager_info_t] WHERE [line_id]=@id";
            dbParams.Add("@id", vLineId, DbType.Int32, ParameterDirection.Input);
            #endregion

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();

                    result.ExecuteCount = conn.Execute(sql, dbParams);
                    result.Status = true;
                    result.Message = "删除线体管理者信息成功.";
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "删除线体管理者信息异常.";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            return result;
        }
        private ExecutionResult<dynamic> GetLineMngrCfgInfo(int vLineId = 0, int vId = 0)
        {
            var result = new ExecutionResult<dynamic>();
            #region sql and parameters
            var param = new DynamicParameters();
            string sql = @"SELECT [id]
                                      ,[line_id]
                                      ,[line_manager]
                                      ,[manager_tel_no]
                                      ,[manager_email]
                                      ,[manager_picture]
                                      ,[line_leader]
                                      ,[leader_tel_no]
                                      ,[leader_email]
                                      ,[leader_picture]
                                      ,[remarks]
                                      ,[is_valid]
                                      ,[create_time]
                                      ,[create_user]
                                      ,[update_time]
                                      ,[update_user]
                                  FROM [dbo].[c_line_manager_info_t] WHERE 1=1 ";
            if (vId > 0)
            {
                sql = sql + "  AND [id]=@id ";
                param.Add("@id", vId, DbType.Int32, ParameterDirection.Input);
            }
            if (vLineId > 0)
            {
                sql = sql + " AND [line_id] = @line_id";
                param.Add("@line_id", vLineId, DbType.Int32, ParameterDirection.Input);
            }
            #endregion 
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if(conn.State != ConnectionState.Open)
                    {
                        conn.Open();
                    }
                    result.Datas = conn.Query(sql, param).ToArray();
                    result.Status = true;
                    result.Message = "获取线体管理者信息成功";
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "获取线体管理者信息异常。";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }

    }
}